//
//  singalListView.m
//  Mdongying
//
//  Created by mac pro -chenweishuai on 14-3-5.
//  Copyright (c) 2014年 伟帅陈. All rights reserved.
//

#import "singalListView.h"
#import "singalLIstViewCell.h"
#import "AppUtility.h"
#define TableViewHeight 35
#define TITLE @"name"
#define ID @"id"

@implementation singalListView
@synthesize myTableArray;

-(void)dealloc
{
    [myTableArray release];
    [myScrollview release];
    [backGroundView release];
    [_one_tb release];
    [super dealloc];
}
- (id)initWithFrame:(CGRect)frame WithSingalListArrays:(NSMutableArray *)SingalListArrays
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        if (SingalListArrays.count *TableViewHeight>200) {
            
            self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, 200);
        }
        else
        {
            self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, SingalListArrays.count *TableViewHeight);
//            self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, 200);

        }
        

        myTableArray = [[NSMutableArray alloc]initWithArray:SingalListArrays];
        backGroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [AppUtility screenWidth],200)];
        backGroundView.userInteractionEnabled = YES;
        backGroundView.backgroundColor = [UIColor clearColor];
        [self addSubview:backGroundView];
        
        myScrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0,0, self.frame.size.width,backGroundView.frame.size.height)];
        myScrollview.backgroundColor = [UIColor whiteColor];
        myScrollview.showsHorizontalScrollIndicator = NO;
        myScrollview.showsVerticalScrollIndicator = NO;
    
        if (SingalListArrays.count *TableViewHeight>300)
        {
            myScrollview.contentSize = CGSizeMake(320/2.5,SingalListArrays.count *TableViewHeight);
        }
        else
        {
            myScrollview.contentSize = CGSizeMake(320/2.5,SingalListArrays.count *TableViewHeight);
        }
        
        [backGroundView addSubview:myScrollview];
        _one_tb = [[TableViewWithBlock alloc]initWithFrame:CGRectMake(0,0, self.frame.size.width,[myTableArray count]*TableViewHeight) style:UITableViewStylePlain];
        _one_tb.backgroundColor = [UIColor clearColor];
        _one_tb.separatorStyle = NO;
        _one_tb.scrollEnabled = NO;
        [myScrollview addSubview:_one_tb];
        
        [_one_tb initTableViewDataSourceAndDelegate:^(UITableView *tableView,int section){
            
            return (NSInteger)[myTableArray count];
            
        } setCellForIndexPathBlock:^(UITableView *tableView,NSIndexPath *indexPath){
            
            
            NSDictionary *dict =[myTableArray objectAtIndex:indexPath.row];
            singalLIstViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"tableCell"];
            
            
            if (!cell)
            {
                
                cell = [[[singalLIstViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"tableCell"] autorelease];
                [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
                cell.backgroundColor = [UIColor clearColor];
                
                [cell setFrame:CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width, TableViewHeight)];
                cell.nameLab.text = [dict objectForKey:TITLE];
                cell.backView.backgroundColor = [UIColor whiteColor];
                
            }
            
            return cell;
            
        } setDidSelectRowBlock:^(UITableView *tableView,NSIndexPath *indexPath){
            
            singalLIstViewCell *selectedcell=(singalLIstViewCell*)[tableView cellForRowAtIndexPath:self.selectedIndexPath];
            
            NSMutableDictionary *listDic = [myTableArray objectAtIndex:indexPath.row];
            
            //TODO:代理回传
            [self.ListDelegate  TouchSingalCellwithListView:self WithDictory:listDic];

            selectedcell.backView.backgroundColor = [UIColor whiteColor];
            selectedcell.leftImageV.backgroundColor = [UIColor clearColor];

            
            singalLIstViewCell *cell=(singalLIstViewCell*)[tableView cellForRowAtIndexPath:indexPath];
            cell.backView.backgroundColor = [UIColor clearColor];
            cell.leftImageV.backgroundColor = [UIColor redColor];
            
            NSIndexPath *index = indexPath;
            self.selectedIndexPath = index;
            
        }];

    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
